package top.aicey.ai.sparring.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.extern.slf4j.Slf4j;
import top.aicey.ai.sparring.common.constants.AIConstants;
import top.aicey.ai.sparring.common.constants.EntityConstants;
import top.aicey.ai.sparring.dto.enums.RoleEnum;
import top.aicey.ai.sparring.entity.SceneInstanceLog;
import top.aicey.ai.sparring.entity.SceneQaPairs;
import top.aicey.ai.sparring.mapper.SceneInstanceLogMapper;
import top.aicey.ai.sparring.mapper.SceneQaPairsMapper;
import top.aicey.ai.sparring.service.ISceneInstanceLogService;

/**
 * <p>
 * 用户陪练日志-log 服务实现类
 * </p>
 *
 * @author 白杨
 * @since 2025-01-14
 */
@Service
@Slf4j
public class SceneInstanceLogServiceImpl extends ServiceImpl<SceneInstanceLogMapper, SceneInstanceLog>
    implements ISceneInstanceLogService {

    @Autowired
    private SceneQaPairsMapper sceneQaPairsMapper;

    @Override
    public String generateAIRequestContent(String moduleId, Long sceneId, String instanceId) {
        List<SceneInstanceLog> list = this.list(Wrappers.query(SceneInstanceLog.class)
            .eq(EntityConstants.MODULE_ID, moduleId)
            .eq(EntityConstants.SCENE_ID, sceneId)
            .eq(EntityConstants.INSTANCE_ID, instanceId)
            .orderByAsc(EntityConstants.ORDER_ID)
        );
        // 如果没有回复或者用户回复为空 则不做评分了
        if (CollectionUtils.isEmpty(list) || !list.stream().anyMatch(v->RoleEnum.USER.equals(v.getRole()))){
            log.info("instanceId:{} log is null",instanceId);
            return null;
        }

        List<SceneQaPairs> qaPairs = sceneQaPairsMapper.selectList(Wrappers
            .query(SceneQaPairs.class)
            .eq(EntityConstants.MODULE_ID, moduleId)
            .eq(EntityConstants.SCENE_ID, sceneId));

        if (CollectionUtils.isEmpty(qaPairs)){
            log.info("scene_id:{} qaPairs is null",sceneId);
            return null;
        }

        Map<Long, String>
            answerMap = qaPairs.stream().collect(Collectors.toMap(SceneQaPairs::getId, SceneQaPairs::getAnswer));

        StringBuffer buffer = new StringBuffer(AIConstants.ROLE_SETTINGS).append(AIConstants.LINE_FEED);

        for (SceneInstanceLog callLog : list) {
            if (RoleEnum.AI.equals(callLog.getRole())){
                String question = callLog.getContent();
                buffer.append(AIConstants.AI_ROLE).append(question).append(AIConstants.LINE_FEED);
            }else if (RoleEnum.USER.equals(callLog.getRole())){
                String content = callLog.getContent();
                buffer.append(AIConstants.USER_ROLE).append(content).append(AIConstants.LINE_FEED);
                buffer.append(AIConstants.REFERENCE_ANSWER_ROLE).append(answerMap.get(callLog.getQaPairId())).append(AIConstants.LINE_FEED);
            }
        }

        return buffer.toString();
    }

    @Override
    public List<SceneInstanceLog> listByInstanceId(String moduleId, String instanceId) {
        List<SceneInstanceLog> list = this.list(Wrappers.query(SceneInstanceLog.class)
            .eq(EntityConstants.MODULE_ID, moduleId)
            .eq(EntityConstants.INSTANCE_ID, instanceId).orderByAsc(EntityConstants.ORDER_ID)
        );
        return list;
    }

}
